package com.example.pbj;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
	static final String SORTSTRING="";
	static final String SORT="";
	static final String CAT="";
	static final String TIME="";
	static final String SEARCH="";
	
	String username;
    String[] sort= { "Most Recent","Oldest" ,"Most Upvotes","Most Downvotes"};
    String[] cat= { "All","Food" ,"Environment","Events","Misc"};
    String[] time= { "Current Month","August 2013" ,"July 2013","June 2013","May 2013"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		
		Spinner sort1 = (Spinner) findViewById(R.id.sort1);
		Spinner cat1 = (Spinner) findViewById(R.id.category1);
		Spinner time1 = (Spinner) findViewById(R.id.time1);
		
		ArrayAdapter<String> a1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, sort);
		ArrayAdapter<String> a2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, cat);
		ArrayAdapter<String> a3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, time);
		a1.setDropDownViewResource(
		           android.R.layout.simple_spinner_dropdown_item);
		a2.setDropDownViewResource(
		           android.R.layout.simple_spinner_dropdown_item);
		a3.setDropDownViewResource(
		           android.R.layout.simple_spinner_dropdown_item);
		sort1.setAdapter(a1);
		cat1.setAdapter(a2);
		time1.setAdapter(a3);
		
		Button submitBtn = (Button) findViewById(R.id.search);
		Button foodBtn = (Button) findViewById(R.id.foodbtn);
		Button eventsBtn = (Button) findViewById(R.id.eventsbtn);
		Button envbtn = (Button) findViewById(R.id.envbutton);
		Button miscBtn = (Button) findViewById(R.id.miscbtn);

		submitBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				EditText search = (EditText) findViewById(R.id.searchinput);
				String searchresult = search.getText().toString();
				Spinner sort1 = (Spinner) findViewById(R.id.sort1);
				Spinner cat1 = (Spinner) findViewById(R.id.category1);
				Spinner time1 = (Spinner) findViewById(R.id.time1);
				
				
				String sort = Integer.toString(sort1.getSelectedItemPosition());
				String cat = (String) cat1.getItemAtPosition(cat1.getSelectedItemPosition());
				String time = (String) time1.getItemAtPosition(time1.getSelectedItemPosition());
				
				Intent myIntent = new Intent(MainActivity.this, Search.class); 
				myIntent.putExtra("SORTSTRING", searchresult);
				myIntent.putExtra("SORT", sort);
				myIntent.putExtra("CAT", cat);
				myIntent.putExtra("TIME", time);
				startActivityForResult(myIntent, 0);
			}
		});
		
		foodBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			
				Intent myIntent = new Intent(MainActivity.this, Food.class); 
				startActivityForResult(myIntent, 0);
			}
		});
		eventsBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			
				Intent myIntent = new Intent(MainActivity.this, Events.class); 
				startActivityForResult(myIntent, 0);
			}
		});
		envbtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			
				Intent myIntent = new Intent(MainActivity.this, Environment.class); 
				startActivityForResult(myIntent, 0);
			}
		});
		miscBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			
				Intent myIntent = new Intent(MainActivity.this, Misc.class); 
				startActivityForResult(myIntent, 0);
			}
		});
		
		return true;
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		
		new getLoginInfo().execute();
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
	    // Handle item selection
	    switch (item.getItemId()) {	
	        case R.id.action_logout:
	        	new Logout().execute();
	        	Intent myIntent3 = new Intent(MainActivity.this, LoginActivity.class);
				startActivityForResult(myIntent3, 0);
				MainActivity.this.finish();
	            return true;
	        case R.id.action_submit:
	        	Intent myIntent4 = new Intent(MainActivity.this, Submit.class);
				startActivityForResult(myIntent4, 0);
				MainActivity.this.finish();
	            return true;
	        case R.id.action_mine:
	        	Intent myIntent5 = new Intent(MainActivity.this, MyPosts.class);
				startActivityForResult(myIntent5, 0);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	//Get Login Info
	private class getLoginInfo extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... nothing) {
			
			PBJDataSource mds2 = new PBJDataSource(MainActivity.this);
			
			mds2.open();
			
			try {
				username = mds2.getUsername();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			mds2.close();
			
			return username;
		}
		
		@Override
		protected void onPostExecute(String whatever) {
			if (username.equals("user")){
				Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
				startActivityForResult(myIntent, 0);
				return;
			}
			else{
				return;
			}
		}
		
	}
	
	//Logout
	private class Logout extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... nothing) {
			
			PBJDataSource mds2 = new PBJDataSource(MainActivity.this);
			
			mds2.open();
			
			try {
				mds2.logoutUser();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			mds2.close();
			
			return "";
		}
		
		@Override
		protected void onPostExecute(String whatever) {
				Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
				startActivityForResult(myIntent, 0);
				MainActivity.this.finish();
		}
		
	}

}
